package system.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import common.config.datasource.DataSourceConstants;
import common.mapper.CommonMysqlSlowSqlMapper;
import common.model.po.MySqlSlowLog;
import common.model.request.base.PaginationRequest;
import org.springframework.stereotype.Service;
import system.model.request.SearchSlowLogRequest;
import system.service.MysqlSlowSqlService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 慢查询实现类
 *
 * @author 米泽鹏
 * @since 2022-03-22
 */
@Service
@DS(DataSourceConstants.MYSQL_DATASOURCE)
public class MysqlSlowSqlServiceImpl extends ServiceImpl<CommonMysqlSlowSqlMapper, MySqlSlowLog> implements MysqlSlowSqlService {

	@Resource
	private CommonMysqlSlowSqlMapper mapper;

	@Override
	public Page<MySqlSlowLog> pageSlowLog(PaginationRequest<SearchSlowLogRequest, MySqlSlowLog> paginationRequest) {
		LambdaQueryWrapper<MySqlSlowLog> queryWrapper = paginationRequest.buildQueryWrapper().lambda();
		// 执行耗时不小于
		BigDecimal minQueryTime = paginationRequest.getParams().getMinQueryTime();
		queryWrapper.ge(Objects.nonNull(minQueryTime), MySqlSlowLog::getQueryTime, minQueryTime);
		// 排序
		queryWrapper.orderByDesc(MySqlSlowLog::getStartTime);
		return page(paginationRequest.buildPage(), queryWrapper);
	}

}
